http = require "http"
url = require "url"

port = 8000

server = http.createServer (req, res) ->
  if req.method != "GET"
    res.writeHead 405
    return res.end()

  url_params = url.parse(req.url)
  console.log url_params
  path = url_params.pathname
  iso = url_params.query.split("=")[1]
  date = new Date iso
  if path == "/api/parsetime"
    result =
      hour: date.getHours()
      minute: date.getMinutes()
      second: date.getSeconds()
  else if path == "/api/unixtime"
    result = 
      unixtime: date.getTime()
  if result
    res.writeHead 200, { 'Content-Type': 'application/json' }
    res.write(JSON.stringify result)
    res.end()
  else
    res.writeHead 404
    res.end()

server.listen(port)
